package com.cloudcross.ssp.web.advertiser.main;

import com.adinall.dsp.core.data.Result;
import com.adinall.dsp.core.data.Success;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.consts.MediaSource;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.service.IAdReportService;
import com.cloudcross.ssp.service.IndexService;
import com.cloudcross.ssp.service.form.IndexForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制面板功能。
 *
 * @author wuqiang.du
 */
@Controller("advertiser.dashboardController")
@RequestMapping("/advertiser/main/dashboard")
public class DashboardController extends SimpleController {

    @Autowired
    private IAdReportService adReportService;

    @Autowired
    private IndexService indexService;

    /**
     * 当首页登录的调用这里，如果登录的是一个广告主账号的话显示的就是一个广告主的余额，如果是一个代理商登录的话显示的就是代理商下所有广告主的余额的总和
     */
    @RequestMapping
    public String index(Model model, HttpServletRequest request) {

        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if (localeResolver == null) {
            throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }

        // 得到登录账号
        Account account = getLoginUser();

        model.addAttribute("loginAccountName", account.getAccountName());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        // 判断一下登录的账号是代理商还是广告主的，如果是广告主则它的代理商id和广告主id不等于0，如果是代理商则代理商的id不等于0广告主id等于0
        if (0 != account.getAdvertiserId() && 0 != account.getAgentId()) {

            paramMap.put("advertiserId", String.valueOf(account.getAdvertiserId()));

            model.addAttribute("type", "advertiser");
            model.addAttribute("advertiserId", account.getAdvertiserId());
            model.addAttribute("accountId", account.getId());
            model.addAttribute("compareMap", findCompareAjax(model, paramMap));
        }

        if (0 != account.getAgentId() && 0 == account.getAdvertiserId()) {
            paramMap.put("agentId", String.valueOf(account.getAgentId()));

            model.addAttribute("type", "agent");
            model.addAttribute("agentId", account.getAgentId());
            model.addAttribute("accountId", account.getId());
            model.addAttribute("compareMap", findCompareAjax(model, paramMap));
        }

        model.addAttribute("hasRight", account.getAgentId() == 1);
        model.addAttribute("sourceList", MediaSource.list());

        return index;
    }

    @ResponseBody
    @RequestMapping("total")
    public Result total(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Account user = (Account) session.getAttribute("userSession");
        return new Success(indexService.total(user.getAdvertiserId(), user.getAgentId()));
    }

    @ResponseBody
    @RequestMapping("money")
    public Result money(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        Account user = (Account) session.getAttribute("userSession");

        return new Success(indexService.money(user.getAdvertiserId(), user.getAgentId()));
    }

    @ResponseBody
    @RequestMapping("stat")
    public Result stat(IndexForm form) {
        return new Success(indexService.stat(form));
    }

    @ResponseBody
    @RequestMapping("listData")
    public Result list(IndexForm form) {
        return new Success(indexService.list(form));
    }

    /**
     * 当对于选定广告主之后查询账户余额和判断箭头等数据
     * 这个方法的功能有两个一个是首页刚登陆之后要找该登陆账号的余额等信息，另一个就是当登陆账号是代理商时，下拉框选则了一个广告主，查询该广告主的余额
     *
     * @param model
     * @param paramMap
     * @return
     */
    @RequestMapping("findCompareAjax")
    protected @ResponseBody
    Map findCompareAjax(Model model, @RequestParam Map<String, Object> paramMap) {
        /**
         * 因为当下拉框如果选则全部的时候传递过来的广告主id位-1，这时其实就是查询该代理商的账号余额
         */
        if (null != paramMap && paramMap.containsKey("advertiserId") && "-1".equals(paramMap.get("advertiserId"))) {
            Account account = getLoginUser();
            paramMap.put("agentId", String.valueOf(account.getAgentId()));
        }

        Map compareMap = adReportService.findCompare(paramMap);
        return compareMap;
    }
}